package org.shan.lis.shy.handler;

import com.google.common.base.Ascii;
import org.shan.lis.common.model.Person;
import org.shan.lis.common.model.PersonResult;
import org.shan.lis.common.model.Result;
import org.shan.lis.common.model.Sample;
import org.shan.lis.utils.ByteBuffUtils;
import org.shan.lis.utils.LisDataUtils;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by amanda.shan on 2019-07-06.
 */
public class ShyDataHandler {

    private byte[] lisDatas = new byte[100];

    private int lisDatasLen = 0;

    private boolean crFlag = false;

    private List<PersonResult> results = new ArrayList<>(); // 检测结果综合

    public List<PersonResult> getResults() {
        return results;
    }

    public boolean decode(ByteBuffer buffer) {

        while (ByteBuffUtils.getSize(buffer) > 0) {

            byte b = ByteBuffUtils.getByte(buffer);
            lisDatas[lisDatasLen++] = b;

            if (b == Ascii.CR) {
                crFlag = true;
            } else if (crFlag) {
                if (b == Ascii.LF) {
                    // 结尾
                    lisDatasLen--;
                    process();
                    lisDatasLen = 0;
                    crFlag = false;

                    return true;
                } else {
                    crFlag = false;
                }
            }
        }

        return false;
    }


    // 临时存储用户信息
    private PersonResult tempPersonResult;

    // 存储样本结果
    private PersonResult.SampleRedult tempSampleRedult;

    private void process() {
        checkSum();

        for (int i = 0; i < lisDatasLen; i++) {
            if (lisDatas[i] == Ascii.ETX) {
                lisDatasLen -= 1;
                break;
            }
        }

        String[] strings = LisDataUtils.split(lisDatas, lisDatasLen);
        if (strings[0].contains("P")) {

            tempPersonResult = new PersonResult();
            results.add(tempPersonResult);

            Person person = decodeP(strings);
            tempPersonResult.setPerson(person);

        } else if (strings[0].contains("O")) {

            if (tempSampleRedult == null){
                tempSampleRedult = new PersonResult.SampleRedult();
                tempPersonResult.getSampleRedults().add(tempSampleRedult);
            }

            if (tempSampleRedult.getSample() != null){

                tempSampleRedult = new PersonResult.SampleRedult();
                tempPersonResult.getSampleRedults().add(tempSampleRedult);
            }

            Sample sample = decodeO(strings);
            tempSampleRedult.setSample(sample);

        } else if (strings[0].contains("R")) {

            if (tempSampleRedult == null){
                tempSampleRedult = new PersonResult.SampleRedult();
                tempPersonResult.getSampleRedults().add(tempSampleRedult);
            }

            if (tempSampleRedult.getResult() != null){
                tempSampleRedult = new PersonResult.SampleRedult();
                tempPersonResult.getSampleRedults().add(tempSampleRedult);
            }

            Result result = decodeR(strings);
            tempSampleRedult.setResult(result);
        }

    }

    private void checkSum() {

    }

    public Person decodeP(String[] strings) {

        Person person = new Person();
        person.setId(strings[2]);
        person.setName(strings[5]);
        person.setBirthday(strings[7]);
        person.setSex(strings[8]);
        person.setRace(strings[9]);
        person.setAttenPhID(strings[13]);

        return person;
    }

    public Sample decodeO(String[] strings) {

        Sample sample = new Sample();
        sample.setId(strings[2]);
        sample.setChannel(strings[4]);
        sample.setTime(strings[7]);
        sample.setCap(strings[9]);
        sample.setType(strings[15]);

        return sample;
    }

    public Result decodeR(String[] strings) {

        Result result = new Result();
        result.setChannel(strings[2]);
        result.setResult(strings[3]);
        result.setUnit(strings[4]);
        result.setError(strings[6]);
        result.setTime(strings[12].substring(0, 14));

        return result;
    }
}
